<template>
  <div class="business-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>业务详情</span>
          <div>
            <el-button @click="goBack">返回</el-button>
            <el-button type="primary" @click="handleEdit">编辑</el-button>
          </div>
        </div>
      </template>

      <el-descriptions :column="1" border>
        <el-descriptions-item label="业务ID">{{ businessDetail.id }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ businessDetail.customer?.name }}</el-descriptions-item>
        <el-descriptions-item label="业务类型">{{ businessDetail.type }}</el-descriptions-item>
        <el-descriptions-item label="业务内容">
          <div style="white-space: pre-line">{{ businessDetail.content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(businessDetail.createTime) }}</el-descriptions-item>
      </el-descriptions>

      <el-divider />

      <h3>关联工单</h3>
      <el-table :data="relatedWorkOrders" border style="width: 100%">
        <el-table-column prop="id" label="工单ID" width="100" />
        <el-table-column prop="region" label="区域" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button link type="primary" @click="viewWorkOrder(row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getBusinessDetail } from '@/api/business'
import { getWorkOrderList } from '@/api/workOrder'
import { formatDate } from '@/utils/date'

const route = useRoute()
const router = useRouter()

const businessId = ref(route.params.id)
const businessDetail = ref({})
const relatedWorkOrders = ref([])
const loading = ref(false)

const statusMap = {
  0: { text: '新建', type: 'info' },
  1: { text: '已回单', type: 'success' },
  2: { text: '已退单', type: 'danger' }
}

const getStatusText = (status) => {
  return statusMap[status]?.text || '未知'
}

const getStatusTagType = (status) => {
  return statusMap[status]?.type || ''
}

const fetchBusinessDetail = async () => {
  try {
    loading.value = true
    const res = await getBusinessDetail(businessId.value)
    businessDetail.value = res
    fetchRelatedWorkOrders()
  } catch (error) {
    ElMessage.error('获取业务详情失败: ' + error.message)
    goBack()
  } finally {
    loading.value = false
  }
}

const fetchRelatedWorkOrders = async () => {
  try {
    const res = await getWorkOrderList({ businessId: businessId.value, size: 100 })
    relatedWorkOrders.value = res.data
  } catch (error) {
    console.error('获取关联工单失败:', error)
  }
}

const handleEdit = () => {
  router.push({ name: 'BusinessDetailEdit', params: { id: businessId.value } })
}

const viewWorkOrder = (id) => {
  router.push({ name: 'WorkOrderDetail', params: { id } })
}

const goBack = () => {
  router.push({ name: 'BusinessList' })
}

onMounted(() => {
  fetchBusinessDetail()
})
</script>

<style scoped>
.business-detail {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>